package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.applets.entity.Address;
import com.example.applets.entity.User;
import com.example.applets.service.impl.AddressServiceImpl;
import com.example.applets.service.impl.UserServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/address")
public class AddressController {
    @Autowired
    private AddressServiceImpl addressService;

    @Autowired
    private UserServiceImpl userService;


    @RequestMapping("/selectAddress")
    @ResponseBody
    //查询单个用户地址
    public AjaxResult selectAddress(@RequestParam("userid") long userid) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userid);
        List<Address> list = addressService.list(wrapper);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据");
        return AjaxResult.success(list);
    }

    @RequestMapping("/add")
    @ResponseBody
    //增加地址
    public AjaxResult addAddress(Address address) {
        System.out.println("值为" + address);
        if (!addressService.save(address))
            return AjaxResult.error("新增失败!");
        return AjaxResult.success("新增成功!");
    }

    @RequestMapping("/select")
    //增加地址
    public String selectOneAddress(@RequestParam("id") long id, Model model) {
        Address address = addressService.getById(id);
        model.addAttribute("a", address);
        return "KAddress/updateAddress";

    }

    @RequestMapping("/update")
    @ResponseBody
    //修改地址
    public AjaxResult updateAddress(Address address) {
        if (!addressService.updateById(address)) {
            return AjaxResult.error("修改失败!");
        }
        return AjaxResult.success("修改成功!");
    }

    @RequestMapping("/delete")
    @ResponseBody
    //修改地址
    public AjaxResult deleteAddress(@RequestParam("ids") long id) {
        if (!addressService.removeById(id)) {
            return AjaxResult.error("删除失败!");
        }
        return AjaxResult.success("删除成功!");
    }

    @RequestMapping("/setDefault")
    @ResponseBody
    //设置默认地址
    public AjaxResult setDefault(@RequestParam("userid") long userid, @RequestParam("defid") long id) {
        User user = userService.getById(userid);
        user.setAddrid(id);
        if (!userService.updateById(user)) {
            return AjaxResult.error("设置默认失败!");
        }
        return AjaxResult.success("设置默认成功!");
    }

    @RequestMapping("/getAdd")
    @ResponseBody
    //获得地址
    public AjaxResult getAddress(@RequestParam("addressid") long id) {
        Address address = addressService.getById(id);
        if (address == null)
            return AjaxResult.error("null", "null");
        return AjaxResult.success("成功", address.getAddr());

    }

}
